#!/usr/bin/env python
#coding=utf-8
# Copyrignt (c) xuzeshui.com. All Rights Reserved.
# Author: Zeshui Xu<i@xuzeshui.com>
# Created Time: 2016-03-09 18:59:37
# Last Modified: 2017-07-21 21:22:06
# File Name: geturl.py
# Description: 

__version__ = "0.1"
__all__ = []

import os
import sys
import hashlib
import json
import copy
import time

host = "127.0.0.1:18008"
salt = "9c9babfb8a3f4bb48d4ea651368585f2"
default_params = {
    "appkey": "271d8700b1074a9582a8cc4cdff94c5f",
    "time": int(time.time()),
    "uid": "2e06b15d2f9847d0b2c6930dbfb9f8f2",
    "ak": "G7IN5tOYT5qp7cdX33v8-A2"
}

def get_url(path, params):
    myparams = copy.deepcopy(default_params)
    myparams.update(params)
    keys = myparams.keys()
    keys.sort()
    calc_arr = []
    for key in keys:
        calc_arr.append(key)
        calc_arr.append(str(myparams[key]))
    calc_arr.append(salt)
    m = hashlib.md5()
    m.update("".join(calc_arr))
    sign = m.hexdigest()
    myparams["sign"] = sign
    qs_arr = []
    for k, v in myparams.iteritems():
        qs_arr.append("%s=%s" % (k, v))
    qs = "&".join(qs_arr)
    return "http://%s%s?%s" % (host, path, qs)

if __name__  == "__main__":
    if len(sys.argv) != 3:
        print "%s [path] [params]" % sys.argv[0]
        sys.exit(1)
    path = sys.argv[1]
    params = json.loads(sys.argv[2])
    print get_url(path, params)
